package com.wxy.gks.springbootprojectday22practice.service.serviceImpl;

import com.wxy.gks.springbootprojectday22practice.bean.Employee;
import com.wxy.gks.springbootprojectday22practice.dao.EmployeeMappe;
import com.wxy.gks.springbootprojectday22practice.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private EmployeeMappe employeeMappe;


    @Override
    public List<Employee> findEmployees() {
//        1.先从redis缓存查询数据:
//        redisTemplate.
        List<Employee> employees = (List<Employee>) redisTemplate.opsForValue().get("employees");

        if(employees!=null&&employees.size()>0){
            System.out.println("使用缓存的数据");
             return employees;
        }else{
             employees= employeeMappe.findEmployees();
             redisTemplate.opsForValue().set("employees",employees);
            System.out.println("使用数据的里面的数据");
             return employees;
        }




    }

    @Override
    public void delete(Integer id) {

        employeeMappe.delete(id);
    }

    @Override
    public Employee getEmpById(Integer id) {
        Employee empById = employeeMappe.getEmpById(id);




        return empById;
    }

    @Override
    public void update(Employee emp) {
        redisTemplate.delete("employees");
        employeeMappe.update(emp);
    }

    @Override
    public void add(Employee emp) {
        redisTemplate.delete("employees");
        employeeMappe.add(emp);
    }


}
